
function initOrderSwap() {
    window.swapByFriendIncome = new auixSwap('#by-friend-income', 'asc', 'desc');
    window.swapByMyIncome = new auixSwap('#by-my-income', 'asc', 'desc');
    window.orderBy = 0;
    window.sort = 0;
}

function orderByFriendIncome(el) {
    var curCls = window.swapByFriendIncome.doSwap();
    tlog(curCls);

    window.orderBy = 1; // 'src_price';
    window.sort = ('desc' == curCls) ? 0 : 1;  // 和css的class名分离开
    addPageLoading();
    requestAndRenderFriendslist(1);

    $api.addCls(el, 'aui-active');
    $api.removeCls($api.byId('by-my-income-frame'), 'aui-active');
}

function orderByMyIncome(el) {
    var curCls = window.swapByMyIncome.doSwap();
    tlog(curCls);

    window.orderBy = 2; // 'price';
    window.sort = ('desc' == curCls) ? 0 : 1;  // 和css的class名分离开
    addPageLoading();
    requestAndRenderFriendslist(1);

    $api.addCls(el, 'aui-active');
    $api.removeCls($api.byId('by-friend-income-frame'), 'aui-active');
}

function requestAndRenderFriendslist(bRefresh) {
    if (bRefresh) {
        window.cur_page = 0;
    }

    var q = {
        query: 'list',
        start: window.cur_page,  // 注意: 是页数，不是起始索引
        count: window.count_per_load,
        orderBy: window.orderBy,
        sort: window.sort
    };
	std_ajax_get('wuchen/friendslist.php', q, {
        always: function() {
            rmPageLoading();
            rmFooterLoading();
			utils.setRefreshDone();
        },
		succ: function(json, url) {
            if (json && json.list && json.list.length > 0) {
                var html = doT_Render('#tpl-friends-list', json.list);
                var dom = $api.byId('friends-list');
                if (bRefresh) {
                    $api.html(dom, html);
                } else {
                    $api.append(dom, html);
                }

                var noF = $api.byId('no-friends');
                if (0 == window.cur_page && !$apix.isHide(noF)) {
                    $apix.addHide(noF);
                }

                sofast();
                window.cur_page++;
            } else {
                if (0 == window.cur_page) {
                    $api.html($api.byId('friends-list'), '');

                    if (window.pageMode > 0) {
                        $api.byId('no-friends-text').innerHTML = '收徒一起赚钱';
                    }
                    var noF = $api.byId('no-friends');
                    $apix.removeHide(noF);
                }

                sofast();
                window.cur_page = -1;
            }
		},
        fail: function(err, url) {
            utils.postErrWithTips(err, url);
        }
	});
}

function opFriend(el) {
    var oper = $api.attr(el, 'data-oper');
    if (1 == oper) { // register
        // 提醒注册
        var to = $api.attr(el, 'data-device');
        var data = {devicetoken: to};
        std_ajax_post_with_query('/wuchen/remind.php', {action: 'register'}, data, {
            succ: function(json) {
            },
            fail: function(err, url) {
                utils.postErrNoTipsEx(err, url, data);
            }
        });
    } else if (8 == oper) { // apprentice
        // 收TA为徒
        onShouTu('shoutu-to-ta');
    }

    var tid = $api.attr(el, 'data-tid');
    var uid = $api.attr(el, 'data-uid');
    var msg = $api.attr(el, 'data-msg');
    if (uid > 0 && str_being(msg)) {
        var msgArr = msg.split(';');
        // 如需分享帖子，在后边追加一条消息
        (tid > 0) && msgArr.push('tid=' + tid);

        for (var i = 0; i < msgArr.length; i++) {
            evt.send('user-send-text-msg', {
                conversationType: 'PRIVATE',
                targetId: uid,
                text: msgArr[i]
            });
        }
    }

    utils.uploadEvent({event: 'remind-friend', op: oper});
    if (tid > 0) {
        shareIt(el);
    } else {
        utils.toast('操作成功');
    }
}

// 收徒
function onShouTu(from) {
    var fromx = from || 'shoutu-earn-togethor';
    utils.uploadEvent({event: fromx});

    window.onLoginDo(function() {
        paintInviteImage({
            succ: function(image, coverImg) {
                views.openShareFrame({
                    shareImg: coverImg,
                    contentUrl: image,
                    type: 5,
                    objectid: 0,
                    openfrom: fromx,
                });
            }
        }, true);
    });
}
